<!--必要的组件说明-->
<!-- -->
<!-- author:yewenjie. date:2024/4/17 11:02-->
<template>
  <div class='List'>
    <div class="search_content">
      <div class="top_title">
        <div class="top_title_back" @click="$router.replace('/registerListV2')"><i class="iconfont icon-back"></i></div>
        <div class="top_title_val">
          {{ $route.meta.title }}
        </div>
      </div>
      <van-search v-model="requestParams.carNo" shape="round" @search="initData" @clear="clearSearch"
        placeholder="请输入登记地点" />
    </div>

    <!-- 列表 -->
    <keep-alive v-if="recordList && recordList.length != 0">
      <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
        <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="loadData"
          :immediate-check='false'>
          <div class="orderList">
            <div class="orderItem" v-for="(item, index) in recordList" :key="index">
              <div class="orderItem_header">
                <div class="orderItem_lt">
                  <div class="carNo">{{ item.createTime }}</div>
                </div>
                <div class="orderItem_rt" @click="$router.push('/registerEditV2?isDetail=0&isList=1&id=' + item.id)">
                  <van-image :src="require('@/assets/images/edit-box.png')" />
                </div>
              </div>
              <div class="orderItem_content">
                <div class="orderItem_data">
                  <i class="iconfont icon-address"></i>
                  <div class="txt">
                    {{ item.accidentAddress && JSON.stringify(item.accidentAddress) != '{}' ? item.accidentAddress : '-'
                    }}
                  </div>
                </div>
              </div>
              <van-grid :border="false" :column-num="4">
                <van-grid-item v-for="(url, index) in forInArr(item.urls)" :key="`image${index}`">
                  <van-image :src="url" width='1.2rem' height='1.2rem' fit="cover" @click='preImg(url)' />
                </van-grid-item>
                <!--                <van-grid-item>
                   <van-image width='1.2rem'
                              height='1.2rem' fit="cover" src="https://img01.yzcdn.cn/vant/apple-2.jpg" />
                 </van-grid-item>
                 <van-grid-item>
                   <van-image width='1.2rem'
                              height='1.2rem' fit="cover" src="https://img01.yzcdn.cn/vant/apple-3.jpg" />
                 </van-grid-item>-->
                <!-- <van-grid-item>
                     <van-image fit="cover" src="https://img01.yzcdn.cn/vant/apple-3.jpg" />
                   </van-grid-item>
                               <van-grid-item @click="fileManage(item)">
                                     <van-image :src="require('@/assets/images/double-right.png')" />
                                   </van-grid-item>-->
              </van-grid>
              <div style="height: auto;width: 100%;text-align: center;border-top: 1px solid #d8d8d8;">
                <van-button class="delete" icon="delete" type="danger"
                  @click="draftDel(item)"><span>删除草稿</span></van-button>
              </div>


            </div>
          </div>
        </van-list>

      </van-pull-refresh>
    </keep-alive>
    <van-empty class="noContent" description="暂无数据" v-else />
  </div>
</template>

<script>
import Vue from 'vue'
import repairApi from "@/api/repairApi";
import { ImagePreview, Dialog, Notify } from 'vant'
export default {
  name: 'List',
  components: {},
  data() {
    return {
      recordList: [
        // {
        //   billId: "1", //单据ID
        //   newCarNo: "闽D5555", //单据车牌号
        //   vinCode: "LIOIAUHHH11111", //车架号
        //   insComp: "平安", //保险公司
        //   ogPsnName: "张三", //客户姓名
        //   fixCenterName: "厦门保时捷售后",
        //   customerPhone: "18778712311", //客户联系电话
        //   accidentAddress: "福建省厦门市思明区展鸿路83号特房波特曼财富中心B座", //出现地点
        //   billStatus: "保存", //单据状态
        //   bizDate: "2023-09-09", //业务日期
        //   createTime: "2023-09-08", //出险日期
        // },
        // {
        //   billId: "1", //单据ID
        //   carNo: "闽D66666", //车辆车牌号
        //   newCarNo: "闽D5555", //单据车牌号
        //   vinCode: "LIOIAUHHH11111", //车架号
        //   insComp: "平安", //保险公司
        //   ogPsnName: "张三", //客户姓名
        //   fixCenterName: "厦门保时捷售后",
        //   customerPhone: "18778712311", //客户联系电话
        //   accidentAddress: "福建省厦门市思明区展鸿路83号特房波特曼财富中心B座", //出现地点
        //   billStatus: "战败", //单据状态
        //   bizDate: "2023-09-09", //业务日期
        //   createTime: "2023-09-08", //出险日期
        // }
      ],
      requestParams: {
        carNo: '', //车架号或者车牌号，String，必填
        pageNo: 1 //当前页数，默认从1开始，每页显示十条，int，不能为空或者小于1
      },
      // 分页参数
      loading: false,
      finished: false,
      refreshing: false,
      isLoadData: false,
    }
  },
  methods: {

    forInArr(str) {
      return str ? str.split(',') : []
    },

    clearSearch() {
      this.requestParams.carNo = ''
      this.initData()
    },
    // 初始化列表
    initData() {
      this.requestParams.pageNo = 1
      this.loadData()
    },
    // 获取列表
    loadData() {
      if (this.isLoadData) return
      this.isLoadData = true
      this.loading = true
      this.finished = false
      console.log(this.isLoadData);
      repairApi
        .draftListPage(this.requestParams)
        .then((res) => {
          if (res) {
            console.info(res)
            if (this.requestParams.pageNo == 1) {
              this.recordList = res.list || []
            } else {
              this.recordList = this.recordList.concat(res.list || [])
            }
            this.requestParams.pageNo += 1

            if (res.list.length < 10) {
              this.finished = true
              this.isLoadData = true
            }
            this.isLoadData = false
            this.loading = false
          }
        })
        .catch((res) => {
          this.isLoadData = false
          this.loading = false
        })
    },
    // 下拉刷新
    onRefresh() {
      console.info('refresh')
      // 清空列表数据
      // this.recordList = []
      this.finished = false
      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.refreshing = false
      this.initData()
    },

    // 图片预览
    preImg(url) {
      let arr = [url]

      ImagePreview({
        images: arr,
        showIndex: true,
        loop: false,
      })
    },

    // 草稿作废
    draftDel(record) {

      Dialog.confirm({
        title: '提示',
        message:
          '您正在删除出险地点为 ' + record.accidentAddress + ' 的草稿记录，请确认。\n\n 确认删除后草稿数据无法恢复',
        messageAlign: 'left',
        // showCancelButton: false
      })
        .then(() => {
          // on confirm
          let param = {
            id: record.id,
            billStatus: 2
          }
          repairApi.draftAddUpdate(param).then((res) => {
            console.info('保存草稿返回：', res)
            this.loading = false;
            if (res && !res.success) {
              Notify({ type: 'danger', message: res.errorMessage })
            } else {
              Notify({ type: 'success', message: '删除成功' })
              // this.recordList 移除记录
              this.recordList = this.recordList.filter(item => item.id !== record.id)
            }

          })
        })
        .catch(() => {
          // on cancel
        });
    },

  },
  filters: {},
  computed: {},
  props: [],
  watch: {},
  created() {
  },
  mounted() {
    this.initData()
  }
}
</script>

<style scoped lang='less'>
.List {
  width: 100%;
  height: 100%;
  background-color: white;

  ///deep/ .van-grid-item:last-child {
  //  flex-basis: 11% !important;
  //  .van-grid-item__content {
  //    padding-left: 0;
  //  }
  //}
  /deep/ .van-grid-item {
    flex-basis: 0 !important;
  }

  .search_content {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 2;

    .top_title {
      position: relative;
      width: 100%;
      height: 50px;
      background: url(../../assets/images/title_bg.png) no-repeat;
      background-size: 100%;
      background-attachment: fixed;

      .top_title_back {
        position: absolute;
        left: 10px;
        line-height: 50px;
        color: #fff;
        font-size: 16px;
      }

      .top_title_val {
        text-align: center;
        line-height: 50px;
        color: #fff;
        font-weight: bold;
        font-size: 16px;
      }

      .top_title_right {
        position: absolute;
        right: 10px;
        top: 0;
        line-height: 50px;
        color: #fff;
        font-size: 16px;
      }
    }
  }

  .top_title {
    position: relative;
    width: 100%;
    height: 50px;
    background: url(../../assets/images/title_bg.png) no-repeat;
    background-size: 100%;
    background-attachment: fixed;

    .top_title_back {
      position: absolute;
      left: 10px;
      line-height: 50px;
      color: #fff;
      font-size: 16px;
    }

    .top_title_val {
      text-align: center;
      line-height: 50px;
      color: #fff;
      font-weight: bold;
      font-size: 16px;
    }
  }

  /deep/ .van-pull-refresh {
    //height: 104px;
    background: #f3f3f3;
    padding-top: 104px;

    .van-list {
      height: 100%;
      overflow-y: auto;
      overflow-x: hidden;
    }
  }

  .orderList {
    //padding-top: 3rem;
    margin: 0.3rem;
    overflow: hidden;

    .orderItem {
      padding: 0.3rem;
      background: #fff;
      border-radius: 10px;
      margin-bottom: 0.3rem;

      .orderItem_header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0.3rem;
        padding-bottom: 0.2rem;
        border-bottom: 1px solid #f6f6f6;

        .orderItem_lt {
          // display: flex;
          align-items: center;
          align-self: center;
          // justify-content: flex-start;

          .carNo {
            font-size: 0.3rem;
            color: #333;
            font-weight: bold;
          }

          .carStatus {
            background: #ffb65e;
            color: #fff;
            border-radius: 50px;
            padding: 0.1rem 0.2rem;
            font-size: 0.24rem;
            text-align: center;
          }
        }

        .orderItem_rt {
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: flex-start;
        }
      }

      .orderItem_content {
        .orderItem_data {
          display: flex;
          justify-content: flex-start;
          align-items: center;
          width: 100%;
          margin-bottom: 0.2rem;
          font-size: 0.28rem;
          color: #333;

          .iconfont {
            font-size: 0.4rem;
            margin-right: 0.2rem;
            color: #999;
          }

          .orderItem_label {
            font-size: 0.28rem;
            margin-right: 0.2rem;
            color: #333;
          }

          .txt {
            color: #101010;
            line-height: 25px;
          }
        }
      }

      .orderItem_file {
        background: #ebebeb;
        color: #333;
        border-radius: 5px;
        padding: 0.2rem 0;
        text-align: center;
        width: 100%;
        font-size: 0.28rem;
        display: flex;
        align-items: center;
        justify-content: center;

        .icon-file {
          font-size: 0.34rem;
          color: #333;
          margin-right: 0.1rem;
        }
      }
    }
  }
}

.delete {
  font-size: 4vw;
  position: relative;
  border: none;
  color: red;
  background: white;
}
</style>
